package com.starview.star.compiler.canonical.compile;

import com.starview.platform.data.type.Location;
import com.starview.star.compiler.cafe.compile.CafeDictionary;
import com.starview.star.compiler.cafe.compile.ISpec;
import com.starview.star.compiler.cafe.compile.VarInfo;
import com.starview.star.compiler.cafe.compile.JavaKind;
import com.starview.star.compiler.canonical.EnvironmentEntry.ContractEntry;
import com.starview.star.compiler.canonical.EnvironmentEntry.ImplementationEntry;
import com.starview.star.compiler.canonical.EnvironmentEntry.ImportEntry;
import com.starview.star.compiler.canonical.EnvironmentEntry.TypeAliasEntry;
import com.starview.star.compiler.canonical.JavaEntry;
import com.starview.star.compiler.canonical.OpenStatement;
import com.starview.star.compiler.canonical.TransformStatement;
import com.starview.star.compiler.canonical.TypeDefinition;
import com.starview.star.compiler.canonical.TypeWitness;
import com.starview.star.compiler.canonical.VarEntry;

public class StatementCompile implements TransformStatement<ISpec, ISpec, ISpec, ISpec, ISpec, CompileContext>
{

  @Override
  public ISpec transformContractDefn(ContractEntry con, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformContractImplementation(ImplementationEntry entry, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformImportEntry(ImportEntry entry, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformJavaEntry(JavaEntry entry, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformTypeAliasEntry(TypeAliasEntry entry, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformTypeEntry(TypeDefinition entry, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformVarEntry(VarEntry entry, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformOpenStatement(OpenStatement open, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public ISpec transformWitness(TypeWitness stmt, CompileContext context)
  {
    // TODO Auto-generated method stub
    return null;
  }// Convert a reference to a free reference
  public static VarInfo varReference(CompileContext cxt, String name, Location loc)
  {
    return varReference(cxt, name, cxt.getDict(), loc);

  }

  private static VarInfo varReference(CompileContext cxt, String name, CafeDictionary dict, Location loc)
  {
    VarInfo var = dict.find(name);

    if (var == null && dict.getParent() != null) {
      VarInfo ref = varReference(cxt, name, dict.getParent(), loc);

      if (ref != null) {
        if (ref.getKind() == JavaKind.builtin || ref.getKind() == JavaKind.constructor)
          return ref;

        if (!ref.isInited())
          cxt.reportError("accessing uninitialized free variable: " + ref + "@" + ref.getLoc(), loc, ref.getLoc());
        return dict.declareFree(ref.getAccess().downGrade(), ref);
      }
    }

    return var;
  }
}
